package com.stud.system.controller.system;

import com.stud.common.constant.Constants;
import com.stud.common.constant.UserConstants;
import com.stud.common.core.domain.AjaxResult;
import com.stud.common.core.domain.Result;
import com.stud.common.core.domain.entity.SysMenu;
import com.stud.common.core.domain.entity.SysUser;
import com.stud.common.core.domain.model.LoginBody;
import com.stud.common.jdbc.MyJdbcTemplate;
import com.stud.common.utils.SecurityUtils;
import com.stud.common.utils.StringUtils;
import com.stud.common.utils.ip.IpUtils;
import com.stud.framework.web.service.SysLoginService;
import com.stud.framework.web.service.SysPermissionService;
import com.stud.system.domain.vo.RouterVo;
import com.stud.system.service.SysMenuService;
import com.stud.system.service.SysRoleService;
import com.stud.system.service.SysUserService;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Set;

/**
 * 登录验证
 *
 * @author Stud
 */
@Tag(name = "登录验证")
@RestController
public class SysLoginController {
    @Autowired
    private SysLoginService sysLoginService;

    @Autowired
    private SysMenuService sysMenuService;

    @Autowired
    private SysPermissionService sysPermissionService;

    @Autowired
    private MyJdbcTemplate jdbcTemplate;

    @Autowired
    private SysRoleService sysRoleService;

    @Autowired
    private SysUserService sysUserService;


    /**
     * 登录方法
     *
     * @param loginBody 登录信息
     * @return 结果
     */
    @PostMapping("/login")
    public AjaxResult login(@RequestBody LoginBody loginBody, HttpServletRequest request) {

        String result = sysLoginService.validateCaptcha(loginBody.getUsername(), loginBody.getCode(), loginBody.getUuid());

        if (result != null) {
            return AjaxResult.error(result);
        }

        if (StringUtils.isBlank(loginBody.getIp())) {
            loginBody.setIp(IpUtils.getIpAddr(request));
        }

        AjaxResult ajax = AjaxResult.success();
        String userType;
        // 生成令牌
        userType = UserConstants.USER_TYPE_NORMAL;
        String token = sysLoginService.login(loginBody, userType, Constants.LOGIN);
        ajax.put(Constants.TOKEN, token);
        return ajax;
    }

    /**
     * 登录方法
     *
     * @param loginBody 登录信息
     * @return 结果
     */
    @PostMapping("/biLogin")
    public AjaxResult biLogin(@RequestBody LoginBody loginBody, HttpServletRequest request) {

        String result = sysLoginService.validateCaptcha(loginBody.getUsername(), loginBody.getCode(), loginBody.getUuid());

        if (result != null) {
            return AjaxResult.error(result);
        }

        if (StringUtils.isBlank(loginBody.getIp())) {
            loginBody.setIp(IpUtils.getIpAddr(request));
        }

        AjaxResult ajax = AjaxResult.success();
        String userType = UserConstants.USER_TYPE_NORMAL;
        String token = sysLoginService.login(loginBody, userType, Constants.BI_LOGIN);
        ajax.put(Constants.TOKEN, token);
        return ajax;
    }

    /**
     * 登录方法
     *
     * @param loginBody 登录信息
     * @return 结果
     */
    @PostMapping("/appLogin")
    public AjaxResult appLogin(@RequestBody LoginBody loginBody, HttpServletRequest request) {

        String result = sysLoginService.validateCaptcha(loginBody.getUsername(), loginBody.getCode(), loginBody.getUuid());

        if (result != null) {
            return AjaxResult.error(result);
        }

//        //小程序登陆先校验是否绑定了微信
//        userService.checkOrSaveWeChatInfo(loginBody.getJsCode(), loginBody.getUsername());

        if (StringUtils.isBlank(loginBody.getIp())) {
            loginBody.setIp(IpUtils.getIpAddr(request));
        }

        AjaxResult ajax = AjaxResult.success();
        String userType = UserConstants.USER_TYPE_NORMAL;
        String token = sysLoginService.login(loginBody, userType, Constants.APP_LOGIN);
        ajax.put(Constants.TOKEN, token);
        return ajax;
    }

    /**
     * 单点登录方法
     *
     * @return 结果
     */
    @PostMapping("/sso")
    public AjaxResult sso(@RequestBody LoginBody loginBody) {
        AjaxResult ajax = AjaxResult.success();
        String token = sysLoginService.sso(loginBody);
        ajax.put(Constants.TOKEN, token);
        return ajax;
    }

    /**
     * 获取用户信息
     *
     * @return 用户信息
     */
    @GetMapping("getInfo")
    public AjaxResult getInfo() {
        SysUser user = SecurityUtils.getLoginUser().getUser();
        // 角色集合
        Set<String> roles = sysPermissionService.getRolePermission(user);
        // 权限集合
        Set<String> permissions = sysPermissionService.getMenuPermission(user);
        AjaxResult ajax = AjaxResult.success();

        user.setPassword(null);
        ajax.put("user", user);
        ajax.put("roles", roles);
        ajax.put("permissions", permissions);
        return ajax;
    }

    /**
     * 获取路由信息(菜单列表)
     *
     * @return 路由信息
     */
    @GetMapping("getRouters")
    public Result<List<RouterVo>> getRouters() {
        Long userId = SecurityUtils.getUserId();
        List<SysMenu> menus = sysMenuService.selectMenuTreeByUserId(userId);
        return Result.ok(sysMenuService.buildMenus(menus));
    }


}
